package com.zoweunion.mechanic.service.base.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zoweunion.mechanic.util.*;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zoweunion.mechanic.dao.base.AuthDao;
import com.zoweunion.mechanic.dao.base.UserDao;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.service.base.AuthService;

/**
 * 授权业务类
 * @author Administrator
 *
 */
@Service
@Transactional
public class AuthServiceImpl implements AuthService {

	@Autowired
	private AuthDao authDao;
	@Autowired
	private UserDao userDao;

	@Override
	@Transactional(readOnly=true)
	public Map<String, Object> getPageQuery(Map<String, Object> reqMap) {
		//1.设置分页的limit和offset
		Map<String, Object> pageMap = Page.appPage(reqMap.get("pageSize").toString(),
				reqMap.get("currentPage").toString());
		reqMap.put("pageSize", pageMap.get("limit"));
		reqMap.put("currentPage", pageMap.get("offset"));
		int limit = Integer.valueOf(pageMap.get("limit").toString());
		//2执行分页查询
		List<Map<String, Object>> list = authDao.pageQuery(reqMap);
		//3.查询总记录数
		int total = authDao.countBycondition(reqMap);
		//int total = list.size();
		//4包装结果集  总条数 总页数 数据集
		HashMap<String, Object> resultMap = new HashMap<String,Object>();
		int pages=(total+limit-1)/limit;//总页数  需要有余进一
		
		resultMap.put("total", total);
		resultMap.put("pages", pages);
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public int insertAuth(Map<String, Object> reqMap) {
		reqMap.put("id",UuidUtil.get32UUID());
		reqMap.put("password",MD5.md5(reqMap.get("password")+""));
		return authDao.insertAuth(reqMap);
	}

	@Override
	public int update(Map<String, Object> reqMap) {
		if(reqMap.get("password")!=null&&!"".equals(reqMap.get("password"))) {
			reqMap.put("password",MD5.md5(reqMap.get("password").toString()));
		}
		return authDao.update(reqMap);
	}

	@Override
	@Transactional(readOnly=true)
	public Map<String, Object> getAuthByUid(Map<String, Object> reqMap) {
		return authDao.getAuthByUid(reqMap);
	}

	@Override
	public int editCreditLimit(Map<String, Object> reqMap) {
		int result=0;
		//1.调用userDao  修改车主授权额度
		reqMap.put("update_user",reqMap.get("current_user_id"));
		result+=userDao.updateUser(reqMap);
		
		//2.创建授权操作日志
		Map<String,Object>authLogMap=new HashMap<String,Object>();
		authLogMap.put("id",UuidUtil.get32UUID());
		authLogMap.put("s_id",reqMap.get("s_id"));
		authLogMap.put("current_user_id",reqMap.get("current_user_id"));
		authLogMap.put("type","2");
//		StringBuilder context=reqMap.get("handler_user_name")+"将车主:"+reqMap.get("user_name")+"的授信额度改为:"+reqMap.get("credit_limit");
		//使用stringBuider进行字符串拼接
		StringBuilder context=new StringBuilder();
		context.append(reqMap.get("handler_user_name")).append("将车主:").append(reqMap.get("user_name")).append("的授信额度改为:").append(reqMap.get("credit_limit"));
		
		authLogMap.put("context",context.toString());
		result+=authDao.addAuthLog(authLogMap);
		return result;
	}
	
	
	
	public ResponseBean editOrderPrice(Map<String, Object> reqMap) {
		
		//1判断是否为管理员操作,非管理员操作要验证授权密码
		boolean flag=true;
		if(!Constants.ROLE_GLY.equals(reqMap.get("r_id"))) {
			reqMap.put("type","1"); //授权类型
			Map<String,Object>authMap=authDao.getAuthByUid(reqMap);
			if(authMap==null) {
				return new ResponseBean(Constants.CODE_HANDEL_FAILED,"授权账号不存在",null);
			}
			if(!authMap.get("password").equals(MD5.md5(reqMap.get("password")+""))) {
				return new ResponseBean(Constants.CODE_HANDEL_FAILED,"授权账号和密码不一致",null);
			}
			
			//用户验证通过  判断修改后的价格是否在授权范围内
			
			if (reqMap.get("upkeepParts") != null && !reqMap.get("upkeepParts").equals("")) {
				List<Map<String, Object>>upkeepPartsList = (List<Map<String, Object>>) reqMap.get("upkeepParts");
				for (Map<String, Object> upkeepPartsMap : upkeepPartsList) {
					if(Integer.valueOf(upkeepPartsMap.get("price").toString())<Integer.valueOf(authMap.get("change_start").toString())
							||Integer.valueOf(upkeepPartsMap.get("price").toString())>Integer.valueOf(authMap.get("change_end").toString())) {
						flag=false;
					}
				}
			
			}
			if (reqMap.get("repairParts") != null && !reqMap.get("repairParts").equals("")) {
				List<Map<String, Object>>repairPartsList = (List<Map<String, Object>>) reqMap.get("repairParts");
				for (Map<String, Object> repairPartsMap : repairPartsList) {
					if(Integer.valueOf(repairPartsMap.get("price").toString())<Integer.valueOf(authMap.get("change_start").toString())
							||Integer.valueOf(repairPartsMap.get("price").toString())>Integer.valueOf(authMap.get("change_end").toString())) {
						flag=false;
					}
				}
				
			}
			
			if(!flag) {
				return new ResponseBean(Constants.CODE_HANDEL_FAILED,"授权额度不在权限范围内",null);
			}
		}
			
		//2.当前为管理员操作   或者是所有验证通过 产生授权操作日志并返回
		if(Constants.ROLE_GLY.equals(reqMap.get("r_id")) || flag) {
			
			if (reqMap.get("upkeepParts") != null && !reqMap.get("upkeepParts").equals("")) {
				List<Map<String, Object>>upkeepPartsList = (List<Map<String, Object>>) reqMap.get("upkeepParts");
				for (Map<String, Object> upkeepPartsMap : upkeepPartsList) {
					Map<String,Object>authLogMap=new HashMap<String,Object>();
					authLogMap.put("id",UuidUtil.get32UUID());
					authLogMap.put("s_id",reqMap.get("s_id"));
					authLogMap.put("current_user_id",reqMap.get("current_user_id"));
					authLogMap.put("type","1");
//					String context=reqMap.get("handler_user_name")+"将工单:"+reqMap.get("order_id")+"中的"+upkeepPartsMap.get("dictionaries_name")+
//							"(编号:"+upkeepPartsMap.get("dictionaries_code")+") 由单价:"+upkeepPartsMap.get("dictionaries_price")+" 改为:"+upkeepPartsMap.get("price");
					StringBuilder context=new StringBuilder();
					context.append(reqMap.get("handler_user_name")).append("将工单:").append(reqMap.get("order_id")).append("中的")
						.append(upkeepPartsMap.get("dictionaries_name")).append("(编号:").append(upkeepPartsMap.get("dictionaries_code"))
						.append(") 由单价:").append(upkeepPartsMap.get("dictionaries_price")).append(" 改为:").append(upkeepPartsMap.get("price"));
					authLogMap.put("context",context.toString());
					authDao.addAuthLog(authLogMap);
				}
			
			}
			if (reqMap.get("repairParts") != null && !reqMap.get("repairParts").equals("")) {
				List<Map<String, Object>>repairPartsList = (List<Map<String, Object>>) reqMap.get("repairParts");
				for (Map<String, Object> repairPartsMap : repairPartsList) {
					Map<String,Object>authLogMap=new HashMap<String,Object>();
					authLogMap.put("id",UuidUtil.get32UUID());
					authLogMap.put("s_id",reqMap.get("s_id"));
					authLogMap.put("current_user_id",reqMap.get("current_user_id"));
					authLogMap.put("type","1");
					StringBuilder context=new StringBuilder();
					context.append(reqMap.get("handler_user_name")).append("将工单:").append(reqMap.get("order_id")).append("中的")
						.append(repairPartsMap.get("dictionaries_name")).append("(编号:").append(repairPartsMap.get("dictionaries_code"))
						.append(") 由单价:").append(repairPartsMap.get("dictionaries_price")).append(" 改为:").append(repairPartsMap.get("price"));
					authLogMap.put("context",context.toString());
					authDao.addAuthLog(authLogMap);
				}
				
			}
		}
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}
	@Override
	
	public String selectUserId(Map<String, Object> reqMap) {
		return authDao.selectUserId(reqMap);
	}

	@Override
	public String getAuthRoleByUseraccount(String user_account) {
		return authDao.getAuthRoleByUseraccount(user_account);
	}

	@Override
	public int updateUserRemainingSum(Map<String, Object> reqMap) {
		
		return authDao.updateUserRemainingSum(reqMap);
	}

	@Override
	public Map<String, Object> getAuthByUserAccount(Map<String, Object> reqMap) {
		return authDao.getAuthByUserAccount(reqMap);
	}


}
